/*
 * name:密码组件 Password
 * author:yanmu.wj@taobao.com
 * date:2011-11-22
 */
KISSY.add('Password',function(S,PasswordCore,Tip){
    var Password = function(cfg){
        //密码文本框
        this.input = cfg.input && S.one(cfg.input);
        //重复密码文本框
        this.reinput = cfg.reinput && S.one(cfg.reinput);
        //强度隐藏域
        this.strengthInput = cfg.strengthInput && S.one(cfg.strengthInput);

        //密码验证tip
        this.tip = cfg.tip && S.one(cfg.tip) ? Tip(cfg.tip) : null;
        this.retip = cfg.retip && S.one(cfg.retip) ? Tip(cfg.retip) : null;

        //密码强度
        this.strength = cfg.strength && S.one(cfg.strength);
        this.strengthCls = cfg.strengthCls ? cfg.strengthCls : {weak:'weak',medium:'medium',strong:'strong'};

        this.on = cfg.on || 'keyup blur';

        //默认tips
        this.defaultTip = cfg.defaultTip || null;
        this.redefaultTip = cfg.redefaultTip || null;
        this.disabledMsg = S.isString(cfg.disabledMsg) ? cfg.disabledMsg : '密码或重复密码格式错误，请重新输入！';

        this.password = null;

        /*
         * 对外接口，获取密码是否可用
         */
        this._disabled = true;
        this._redisabled = !!this.reinput;
        this.disabled = this._disabled || this._redisabled;
    };
    var _Password = function(cfg){
        return new Password(cfg);
    };
    S.augment(Password,{
        Message:{
            SIZE:'密码长度应为6-16个字符！',
            ILLEGAL:'密码只能是数字、字母以及!@#$%^&*?_.-~中的任意字符！',
            SAME:'密码不能为同一字符！',
            ALLLETTER:'密码不能全为字母！',
            ALLNUMBER:'密码不能全为数字！'
        },
        init:function(){
            if(!this.input || !this.tip) return this;

            var self = this;

            this.checkReDisabled(true);

            this.input.on(this.on,function(){
                self.password = PasswordCore(self.input.val());
                self.checkAble();
                self.checkStrength();
            });

            if(!this.reinput || !this.retip) return this;
            this.reinput.on(this.on,function(){
                self.checkRe();
            });

            return this;
        },
        //密码可用性校验
        checkAble:function(){

            var p = this.password,
                t = this.tip;
            var disabled = '';


            if(p.size() == 0){
                disabled = 'DEFAULT'
            }else if(p.isIllegal()){
                disabled = 'ILLEGAL';
            }else if(p.isAllSame()){
                disabled = 'SAME';
            }else if(p.isAllLetter()){
                disabled = 'ALLLETTER';
            }else if(p.isAllNumber()){
                disabled = 'ALLNUMBER';
            }else if(p.size() < 6 && p.size() > 0){
                disabled = 'SIZE';
            }

            this._disabled = !!disabled;
            this.disabled = this._disabled || this._redisabled;

            //更新tip
            switch(disabled){
                case '':
                    t.ok();
                    break;
                case 'DEFAULT':
                    this.resetTip();
                    break;
                case 'SIZE':
                case 'ILLEGAL':
                case 'SAME':
                case 'ALLLETTER':
                case 'ALLNUMBER':
                    t.error(this.Message[disabled]);
                    break;
            }

            //重复密码输入框是否可用
            this.checkRe();
            this.checkReDisabled();
        },
        //密码强度校验
        checkStrength:function(){
            if(!this.input || !this.strength) return;

            var p = this.password,
                cls = this.strengthCls,
                stength = 1;
            var level = p.level();
            
            if(level>=6){
                this.strength.removeClass(cls.weak).removeClass(cls.medium).addClass(cls.strong);
                strength = 3;
            }else if(level>=5){
                this.strength.removeClass(cls.weak).removeClass(cls.strong).addClass(cls.medium);
                strength = 2;
            }else{
                this.strength.removeClass(cls.strong).removeClass(cls.medium).addClass(cls.weak);
                strength = 1;
            }

            //设置强度隐藏域
            if(this.strengthInput) this.strengthInput.val(strength);
        },
        resetTip:function(){
            this.resetReTip();
            if(!this.tip) return;
            if(this.defaultTip && this.defaultTip.type && this.defaultTip.msg){
                this.tip.change(this.defaultTip.type,this.defaultTip.msg);
            }else{
                this.tip.hide();
            }
        },
        //重复密码校验
        checkRe:function(){
            if(!this.reinput.val().length){
                this._redisabled = true;
                this.resetReTip();
            }else if(this.input.val()===this.reinput.val()){
                this._redisabled = false;
                this.retip.ok();
            }else{
                this._redisabled = true;
                this.retip.error('两次输入的密码不一致！');
            }
            this.disabled = this._disabled || this._redisabled;
        },
        checkReDisabled:function(disabled){
            this.reinput && this.reinput.prop('disabled',S.isBoolean(disabled) ? disabled : this._disabled);
            this._disabled && this.resetReTip();
        },
        resetReTip:function(){
            if(!this.retip) return;
            if(this._disabled){
                this.retip.hide();
            }else if(this.redefaultTip && this.redefaultTip.type && this.redefaultTip.msg){
                this.retip.change(this.redefaultTip.type,this.redefaultTip.msg);
            }else{
                this.retip.hide();
            }
        }
    });
    return _Password;
},{
    requires:['PasswordCore','Message']
});
